package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.MemberAgeStatistics;

import model.Member;

import factory.DaoFactory;

/**
 * Servlet implementation class MemberAgeStatisticsServlet
 */
@WebServlet("/MemberAgeStatisticsServlet")
public class MemberAgeStatisticsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberAgeStatisticsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int belowEighteen = 0;
		int eighteenToThirty = 0;
		int thirtyToForty = 0;
		int fortyToFifty = 0;
		int aboveFifty = 0;
		List<Member> list = DaoFactory.getMemberDao().findAll();
		for (int i = 0; i < list.size(); i++) {
			Member member = list.get(i);
			if (member.getAge() < 18) {
				belowEighteen++;
			} else if (member.getAge() < 30) {
				eighteenToThirty++;
			} else if (member.getAge() < 40) {
				thirtyToForty++;
			} else if (member.getAge() < 50) {
				fortyToFifty++;
			} else {
				aboveFifty++;
			}
		}
		MemberAgeStatistics statistics = new MemberAgeStatistics(belowEighteen, eighteenToThirty, thirtyToForty, fortyToFifty, aboveFifty);
		request.setAttribute("statistics", statistics);
		String address ="/jsp/MemberAgeStatistics.jsp";
		RequestDispatcher dispatcher =request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
